package com.yiduo.outpost.core.model.exception;

import com.yiduo.outpost.core.model.result.ResultCodeEnum;
import jakarta.annotation.Nonnull;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;

import java.util.Objects;

/**
 * Basic restful exception class.
 */
@ResponseStatus(HttpStatus.BAD_REQUEST)
public class RestException extends RuntimeException {

    private static final long serialVersionUID = 1L;

    private ResultCodeEnum resultCodeEnum;

    public Integer code;

    /**
     * The replacement values for placeholders,
     */
    protected Object[] args;

    public RestException(String message) {
        super(message);
    }

    public RestException(@Nonnull ResultCodeEnum resultCodeEnum){
        super(resultCodeEnum.getMessage());
        this.resultCodeEnum = resultCodeEnum;
        this.code = resultCodeEnum.getCode();
    }

    public RestException(Integer code, String message) {
        super(message);
        this.code = code;
    }

    public RestException(String message, Object... args) {
        super(message);
        this.args = args;
    }

    public RestException(String message, Throwable cause, Object... args) {
        super(message, cause);
        this.args = args;
    }

    public Object[] getArgs() {
        return this.args;
    }

    public Integer getCode() {
        if (Objects.isNull(code)) {
            code = HttpStatus.BAD_REQUEST.value();
        }
        return code;
    }
}
